// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include "Components/ControllerComponent.h"
#include "Inventory/LyraInventoryItemInstance.h"

#include "LyraQuickBarComponent.generated.h"

class AActor;
class ULyraEquipmentInstance;
class ULyraEquipmentManagerComponent;
class UObject;
struct FFrame;

UCLASS(Blueprintable, meta=(BlueprintSpawnableComponent))
class ULyraQuickBarComponent : public UControllerComponent
{
	GENERATED_BODY()

public:
	ULyraQuickBarComponent(const FObjectInitializer& ObjectInitializer = FObjectInitializer::Get()); // 构造函数

	UFUNCTION(BlueprintCallable, Category="Lyra")
	void CycleActiveSlotForward(); // 向前循环活动槽位

	UFUNCTION(BlueprintCallable, Category="Lyra")
	void CycleActiveSlotBackward(); // 向后循环活动槽位

	UFUNCTION(Server, Reliable, BlueprintCallable, Category="Lyra")
	void SetActiveSlotIndex(int32 NewIndex); // 设置活动槽位索引

	UFUNCTION(BlueprintCallable, BlueprintPure=false)
	TArray<ULyraInventoryItemInstance*> GetSlots() const // 获取所有槽位
	{
		return Slots;
	}

	UFUNCTION(BlueprintCallable, BlueprintPure=false)
	int32 GetActiveSlotIndex() const { return ActiveSlotIndex; } // 获取活动槽位索引

	UFUNCTION(BlueprintCallable, BlueprintPure = false)
	ULyraInventoryItemInstance* GetActiveSlotItem() const; // 获取活动槽位物品

	UFUNCTION(BlueprintCallable, BlueprintPure=false)
	int32 GetNextFreeItemSlot() const; // 获取下一个空闲物品槽位

	UFUNCTION(BlueprintCallable, BlueprintAuthorityOnly)
	void AddItemToSlot(int32 SlotIndex, ULyraInventoryItemInstance* Item); // 添加物品到槽位

	UFUNCTION(BlueprintCallable, BlueprintAuthorityOnly)
	ULyraInventoryItemInstance* RemoveItemFromSlot(int32 SlotIndex); // 从槽位移除物品

	virtual void BeginPlay() override; // 开始游戏

private:
	void UnequipItemInSlot(); // 卸下槽位中的物品
	void EquipItemInSlot(); // 装备槽位中的物品

	ULyraEquipmentManagerComponent* FindEquipmentManager() const; // 查找装备管理器

protected:
	UPROPERTY()
	int32 NumSlots = 3; // 槽位数量

	UFUNCTION()
	void OnRep_Slots(); // 复制通知：槽位

	UFUNCTION()
	void OnRep_ActiveSlotIndex(); // 复制通知：活动槽位索引

private:
	UPROPERTY(ReplicatedUsing=OnRep_Slots)
	TArray<TObjectPtr<ULyraInventoryItemInstance>> Slots; // 槽位数组

	UPROPERTY(ReplicatedUsing=OnRep_ActiveSlotIndex)
	int32 ActiveSlotIndex = -1; // 活动槽位索引

	UPROPERTY()
	TObjectPtr<ULyraEquipmentInstance> EquippedItem; // 已装备的物品
};

/**
 * 快速栏槽位更改消息
 */
USTRUCT(BlueprintType)
struct FLyraQuickBarSlotsChangedMessage
{
	GENERATED_BODY()

	UPROPERTY(BlueprintReadOnly, Category=Inventory)
	TObjectPtr<AActor> Owner = nullptr; // 所有者

	UPROPERTY(BlueprintReadOnly, Category = Inventory)
	TArray<TObjectPtr<ULyraInventoryItemInstance>> Slots; // 槽位数组
};

/**
 * 快速栏活动索引更改消息
 */
USTRUCT(BlueprintType)
struct FLyraQuickBarActiveIndexChangedMessage
{
	GENERATED_BODY()

	UPROPERTY(BlueprintReadOnly, Category=Inventory)
	TObjectPtr<AActor> Owner = nullptr; // 所有者

	UPROPERTY(BlueprintReadOnly, Category=Inventory)
	int32 ActiveIndex = 0; // 活动索引
};